package com.chat.datea.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.SingerListAdapter;
import com.chat.datea.bean.HotSingerEntity;
import com.chat.datea.utils.fragment.ReplaceFragment;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周橹 on 15/10/21.
 */
public class SingerListFragment extends Fragment {

    private String url;
    private ImageView back;
    private TextView back_text;
    private ListView singer_listView;
    private SingerListAdapter singerListAdapter;
    private List<HotSingerEntity.ArtistEntity> artistEntities;
    private VolleySingleton volleySingleton;

    // 通过构造方法不同传入不同的url，解析不同的数据
    public SingerListFragment(String url) {
        this.url = url;
        Log.e("SingerListFragment", url);
        volleySingleton = VolleySingleton.getVolleySingleton(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.singer_listview, null);
        initView(view);
        getData();
        return view;
    }

    private void initView(View view) {
        back = (ImageView) view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });

        back_text = (TextView) view.findViewById(R.id.back_text);
        back_text.setText("歌手列表");
        singer_listView = (ListView) view.findViewById(R.id.singer_listView);

    }

    private void getData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                artistEntities = new ArrayList<>();
                HotSingerEntity hotSingerEntity = gson.fromJson(response, HotSingerEntity.class);
                Log.i("chouru", "-----" + hotSingerEntity);
                artistEntities = hotSingerEntity.getArtist();
                singerListAdapter = new SingerListAdapter(getActivity(), artistEntities);
                singer_listView.setAdapter(singerListAdapter);

                singer_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        GuessSingerFragment singerFragment = new GuessSingerFragment(getActivity().getSupportFragmentManager(), artistEntities.get(position).getTing_uid(), artistEntities.get(position).getArtist_id(), artistEntities.get(position).getName());
                        new ReplaceFragment(singerFragment,getActivity().getSupportFragmentManager(),R.id.fl_content,true,null);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request, "SingerListFragment");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        volleySingleton.removeRequest("SingerListFragment");
    }
}
